package com.mj.express.service;

import java.io.IOException;
import java.util.Random;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import com.mj.express.bean.Kuaidi100JSON;
import com.mj.express.util.HttpUtils;

/**
 * 快递查询
 * @author zhaominglei
 * @date 2015-3-16
 * 
 */
public class ExpressService extends BaseService {
	@SuppressWarnings("unused")
	private static final String TAG = ExpressService.class.getSimpleName();
	private static String KUAIDI100_URL = "http://www.kuaidi100.com/query"; //kuaidi100
	
	public Kuaidi100JSON getExpressInfo(String companytype, String expressno) {
		if (companytype == null || companytype.equals("")
				|| expressno == null || expressno.equals("")) {
			return null;
		}
		StringBuilder queryURL = new StringBuilder(KUAIDI100_URL);
		queryURL.append("?type=").append(companytype).append("&postid=").append(expressno);
		queryURL.append("&id=1&valicode=&temp=").append(new Random().nextDouble());
		String html = HttpUtils.doGetForKuaidi100(queryURL.toString(), null);
		if (html != null && !html.equals("")) {
			try {
				Kuaidi100JSON kuaidi100json = new ObjectMapper().readValue(html, Kuaidi100JSON.class);
				return kuaidi100json;
			} catch (JsonParseException e) {
				e.printStackTrace();
			} catch (JsonMappingException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
